package qz_rpt


import java.text.SimpleDateFormat

class FinalPassYieldByMonthController {

    def commonSqlService
    def dynamicQueryService

    def index(Integer max) {
        def cellList = org.kdx.DataUtils.getCellList()
        def pageSizeList = org.kdx.DataUtils.getPageSizeList()

        [totalCount: 5, pageSize: 20, pageCurrent: 1, orderField: 'id', orderDirection: 'asc', pageSizeList: pageSizeList, cellList: cellList]
    }

    def finalPassYieldByMonth() {
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 100
        params.startno = ((pageCurrent - 1) * pageSize) + 1
        params.endno = pageCurrent * pageSize
        def finalPassYieldByMonth = []
        def pageSizeList = [10, 20, 50, 100]
        def totalCount = 0
        def tableHeadList = []
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime()))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date()))
        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)
//        def customerLotIdList = commonSqlService.customerLotIdList(params)
        params.SYS_QUERY_NAME = 'checkStepList'
        def checkStepList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'partDirect'
        def partDirect = dynamicQueryService.queryForList(params)
        List<Integer, Integer> qtyList = new ArrayList<>()
        if (params.EXPORT_CONTENT_NAME == 'Excel') {
            params.SYS_QUERY_NAME = 'finalPassYieldByMonth'
            finalPassYieldByMonth = dynamicQueryService.queryForList(params)
            def inQty = 0
            def outQty = 0
            def yield = 0.0
            for (int i = 0; i < finalPassYieldByMonth.size(); i++) {
                inQty += finalPassYieldByMonth[i].检验数 == null ? 0 : finalPassYieldByMonth[i].检验数
                outQty += finalPassYieldByMonth[i].正常品合格数 == null ? 0 : finalPassYieldByMonth[i].正常品合格数
            }
            yield = inQty == 0 ? 0 :(outQty / inQty * 100)
            yield = qz_rpt.DataUtil.saveOneBitTwoRound(yield)
            qtyList.add(0, inQty)
            qtyList.add(1, outQty)
            qtyList.add(2, yield)
            if (finalPassYieldByMonth.size() > 0) {
                tableHeadList = finalPassYieldByMonth[0].keySet()
            }
            params.SYS_QUERY_NAME = 'finalPassYieldByMonthCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        }


        [pageSize             : pageSize, pageSizeList: pageSizeList, startno: params.startno ?: '', endno: params.endno ?: '', partDirect: partDirect,
         totalCount           : totalCount, tableHeadList: tableHeadList, customerDirect: params.customerDirect ?: '',
//         customerLotIdList:customerLotIdList,
         qtyList:qtyList,
         group4               : params.group4 ?: '', grade2: params.grade2 ?: '', partType: params.partType ?: '', stepDesc: params.stepDesc ?: '',
         startTime            : params.startTime ?: '', endTime: params.endTime ?: '', checkStepList: checkStepList, customerLotId: params.customerLotId ?: '',
         finalPassYieldByMonth: finalPassYieldByMonth, defectType: params.defectType ?: '']

    }
}
